package com.tos.second.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tos.second.common.constant.MessageConstant;
import com.tos.second.common.exception.BaseException;
import com.tos.second.common.exception.DeletionNotAllowedException;
import com.tos.second.dto.CategoryAddDto;
import com.tos.second.entity.Category;
import com.tos.second.entity.Goods;
import com.tos.second.mapper.CategoryMapper;
import com.tos.second.service.CategoryService;
import com.tos.second.service.GoodsService;
import com.tos.second.vo.CategoryListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;


    @Autowired
    private GoodsService goodsService;

    /**
     * 获取分类列表
     * @return
     */
    @Override
    public List<CategoryListVo> getCategory() {
//        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
//
//        queryWrapper.orderByDesc(Category::getSort);

        List<Category> list = categoryMapper.getList();
        return list.stream().map(item->{
            CategoryListVo categoryListVo = new CategoryListVo();
            BeanUtils.copyProperties(item, categoryListVo);
            return categoryListVo;
        }).toList();
    }

    /**
     * 添加分类
     * @param categoryAddDto
     */
    @Override
    public void addCategory(CategoryAddDto categoryAddDto) {
        Category category = new Category();
        //复制属性进行添加
        BeanUtils.copyProperties(categoryAddDto, category);
        //其他属性自动赋值，直接插入
        categoryMapper.saveCategory(category);
//        save(category);
    }

    /**
     * 删除分类
     * @param id
     */
    @Override
    public void deleteCategory(Long id) {
        //根据id获得分类
//        Category category = getById(id);
        Category category = categoryMapper.getCategoryById(id);
        //判断分类是否为空
        if(category == null){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_NOT_EXIT);
        }

        //判断是否为启用状态
        if (category.getStatus() == 1){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_IS_USING);
        }

        //判断分类下是否含有其他商品，如果有，不能删除
        Long categoryId = category.getId();
        //创造查询条件
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Goods::getCategoryId,categoryId);
        List<Goods> list = goodsService.list(queryWrapper);
        if (list != null && !list.isEmpty()){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_HAS_GOODS);
        }

        //全部校验完成，可进行删除
        categoryMapper.deleteCategoryById(id);
//        removeById(id);
    }

    @Override
    public Category getCategoryById(Long id) {
        if (id == null){
            throw new BaseException("id不能为空");
        }
        //查询数据库
        return categoryMapper.getCategoryById(id);
    }

}
